<?php
class Javascript{
	private $comment = "";
	private $js = "";

	public function __construct(){
	}

	public function min(){
		require_once 'jsmin.php';
		$jsMin = new JSMin($this->js);
		$this->js = $jsMin->minify();
		return $this;
	}

	public function pack(){
		require_once 'class.JavaScriptPacker.php';
		$packer = new JavaScriptPacker($this->js);
		$this->js = $packer->pack();
		return $this;
	}

	public function rhino(){
		$tmp = '/tmp/moo.tmp.js';
		file_put_contents($tmp, $this->js);
		$this->js = `java -jar ./lib/custom_rhino.jar -c $tmp 2>&1`;
		return $this;
	}

	public function addFile($file){
		$this->addSource(file_get_contents($file));
	}

	public function addSource($source){
		$this->js .= "\n$source";
	}

	public function addComment($comment){
		$this->comment .= $comment;
	}

	public function __toString(){
		$comment = "";
		$lines = preg_split("/[\\n\\r]+/", $this->comment);
		if(strlen($this->comment) > 0){
			$comment = "// " . implode("\n// ", array_filter($lines)) . "\n";
		}
		return $comment . $this->js;
	}
	
	public function getSize(){
		return strlen($this->__toString());
	}
}
?>